package cn.wolfcode.luowowo.member.service.impl;

import cn.wolfcode.luowowo.cache.service.IUserInfoRedisService;
import cn.wolfcode.luowowo.common.exception.DisableException;
import cn.wolfcode.luowowo.common.util.AssertUtil;
import cn.wolfcode.luowowo.common.util.Consts;
import cn.wolfcode.luowowo.member.domain.UserInfo;
import cn.wolfcode.luowowo.member.mapper.UserInfoMapper;
import cn.wolfcode.luowowo.member.service.IUserInfoService;
import com.alibaba.druid.sql.visitor.functions.If;
import com.alibaba.dubbo.config.annotation.Reference;
import com.alibaba.dubbo.config.annotation.Service;
import org.apache.shiro.crypto.hash.Md5Hash;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.DigestUtils;

import java.util.List;
import java.util.UUID;

/**
 * Created by LENOVO on 2019/8/7.
 */


@Service
public class UserInfoServiceImpl implements IUserInfoService {

    @Autowired
    private UserInfoMapper userInfoMapper;

    @Reference
    private IUserInfoRedisService userInfoRedisService;

    public UserInfo get(Long id) {
        return userInfoMapper.selectByPrimaryKey(id);
    }

    /**
     *输入电话号码,判断是否存在
     * @param phone
     * @return
     */
    @Override
    public boolean checkPhone(String phone) {
        return userInfoMapper.selectCountByPhone(phone) >0;//如果查询出来有数量,就证明有这个电话,返回一个true
    }

    @Override
    public void sendVerifyCode(String phone) {
    //创建手机验证码
        String code = UUID.randomUUID().toString().substring(0, 4);
        //缓存手机验证码
        userInfoRedisService.setVerfiyCode(phone,code);

     //发送手机验证码 ()假的发送
        StringBuilder sq = new StringBuilder(100);
        sq.append("您的验证码是:").append(code).append(",请在").append(Consts.VERIFY_CODE_VAI_TIME).append("分钟内使用");

        System.out.println(sq.toString());
    }

    /**
     *进行注册
     * @param phone
     * @param nickname
     * @param password
     * @param rpassword
     * @param verifyCode
     */
    @Override
    public void userRegist(String phone, String nickname, String password, String rpassword, String verifyCode) {
        //注册之前我们需要对传入的数值进行验证

        //必须效验传入进来的参数是否为空
        AssertUtil.hasLength(phone,"手机号码不能为空");
        AssertUtil.hasLength(nickname,"昵称不能为空");
        AssertUtil.hasLength(password,"密码不能为空");
        AssertUtil.hasLength(rpassword,"确认密码不能为空");
        AssertUtil.hasLength(verifyCode,"验证码不能为空");
        //必须效验手机号码是否存在
        if(userInfoMapper.selectCountByPhone(phone) > 0){
                throw  new RuntimeException("手机号码已经被注册了");
        }
        //必须效验两次密码是否一致
        AssertUtil.isEquals(password,rpassword,"两次输入的密码不同");

        //必须效验短信验证码是否正确
        String redisCode = userInfoRedisService.getVerfiyCode(phone);
        System.out.println(redisCode);
        if(!verifyCode.equalsIgnoreCase(redisCode)){
            throw  new RuntimeException("验证码不一致或者失效了");
        }
        //使用MD5加密
            Md5Hash md5password = new Md5Hash(password,phone);
        System.out.println(md5password);

        //才能注册
        UserInfo userInfo = new UserInfo();
        userInfo.setPhone(phone);
        userInfo.setPassword(String.valueOf(md5password));
        userInfo.setNickname(nickname);
        userInfo.setLevel(1);  //默认级别1
        userInfo.setState(userInfo.STATE_NORMAL);//設置默认的级别
        userInfo.setHeadImgUrl("/images/default.jpg");
        userInfoMapper.insert(userInfo);
    }

    /**
     *  登录操作
     *  返回一个token的字符串,然后再controller中操作
     * @param username
     * @param password
     */
    @Override
    public String userLogin(String username, String password) {
        //判断账号或者密码是否为空
        AssertUtil.hasLength(username,"账号不能为空");
        AssertUtil.hasLength(password,"密码不能为空");
        //检查账号或者密码是否正确
        //把密码进行md5解析
        String md5password = String.valueOf(new Md5Hash(password,username));
        UserInfo userInfo = userInfoMapper.selectByUsernameAndPassword(username, md5password);

        //检查是否存在
        if (userInfo==null){
            throw new DisableException("账号(手机号)或者密码错误");
        }

        //如果存在就存入一个token到redis中
        //先设计一个token
        String token = UUID.randomUUID().toString().replace("-", "");//replace 就是UUID中不能出出现空和

        userInfoRedisService.addToken(token,userInfo);
        return token;
    }

    @Override
    public List<UserInfo> list() {

        return userInfoMapper.selectAll();
    }
}
